package com.dominator.repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.dominFramework.core.typewrap.Dtos;
import com.dominator.mybatis.dao.*;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.dao.JdbcHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dominFramework.core.dao.SqlDao;
import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;


@Repository
public class DaoOrder {
	
	@Resource
	JdbcHelper jh;
	
	@Autowired
	T_orderDao orderDao;

	@Autowired
	T_order_noticeDao orderNoticeDao;
	
	@Autowired
	SqlDao sqlDao;
	
	@Autowired
	DaoAccountTicket accountTicketDao;

	@Autowired
	T_user_upgradeDao tUserUpgradeDao;



	

	/**
	 * 添加余额
	 * @return
	 */
	public int addMoney(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,money,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+dto.getBigDecimal("money"));
		order.setPoints(dto.getBigDecimal("points"));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(dto.getBigDecimal("money"));
		notice.setPoints(BigDecimal.ZERO);
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");// 0
		notice.setMemo(dto.getString("title")+"："+dto.getBigDecimal("money"));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("1");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}



	/**
	 * 福分微信充值
	 * @return
	 */
	public int pointsRecharge(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,points,paymentCode,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+dto.getBigDecimal("points"));
        order.setPoints(dto.getBigDecimal("points"));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO);
		notice.setPoints(dto.getBigDecimal("points"));
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code(dto.getString("paymentCode"));//微信1
		notice.setMemo(dto.getString("title")+"："+dto.getBigDecimal("points"));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("0");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}

	/**
	 * 福分添加
	 * @return
	 */
	public int addPoints(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,money,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+dto.getBigDecimal("points"));
		order.setPoints(dto.getBigDecimal("points"));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO);
		notice.setPoints(dto.getBigDecimal("points"));
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");
		notice.setMemo(dto.getString("title")+"："+dto.getBigDecimal("points"));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("1");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}


	/**
	 * 会员升级(续费)送积分
	 * @return
	 */
	public int upgradeAddPoints(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,points,typeId,sn,upgrade");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+dto.getBigDecimal("points"));
		order.setPoints(dto.getBigDecimal("points"));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		String orderNoticeId=SystemIdUtils.uuid();
		notice.setId(orderNoticeId);
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO);
		notice.setPoints(dto.getBigDecimal("points"));
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");
		notice.setMemo(dto.getString("title")+"："+dto.getBigDecimal("points"));
		notice.setTrade_id(dto.getString("typeId"));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("0");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		Integer res=orderNoticeDao.insert(notice);
		if ("10".equals(dto.getString("typeId"))||"13".equals(dto.getString("typeId"))){
            T_user_upgradePO tUserUpgradePO =new T_user_upgradePO();
            tUserUpgradePO.setId(SystemIdUtils.uuid());
            tUserUpgradePO.setUser_id(dto.getString("userId"));
            tUserUpgradePO.setUpgrade(dto.getString("upgrade"));
            tUserUpgradePO.setIs_paid("0");
            tUserUpgradePO.setCreate_time(new Date());
            tUserUpgradePO.setIs_valid("1");
			tUserUpgradePO.setOrder_notice_id(orderNoticeId);
            res=tUserUpgradeDao.insert(tUserUpgradePO);
		}
		return res;
	}


	/**
	 * 会员余额消费
	 * @return
	 */
	public int consumption(Dto dto){
		return pointsRecharge(dto);
	}




	/**
	 * 兑换积分
	 * @param
	 * @param accountId
	 * @param
	 * @return
	 */
	public int exchangePoints(String userId,String accountId, BigDecimal points,String exchangeCode){
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(userId);
		order.setIs_settle("0");
		order.setMemo("兑换福分： "+points);
        order.setPoints(points);
		order.setType_id("1");
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(userId);
		notice.setTitle("用户 兑换福分");
		notice.setPoints(points);
		notice.setAccount_id(accountId);
		notice.setPayment_code("1");//微信1
		notice.setMemo("用户兑换："+points+"福分");
		notice.setOrder_type("1");
		notice.setIs_paid("0");
		notice.setSn(exchangeCode);
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}


	/**
	 * 消耗积分
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public int deduPoints(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,points,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		order.setPoints(BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO);
		notice.setPoints(BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");
		notice.setMemo(dto.getString("title")+":"+BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("0");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}



	/**
	 * 提现
	 * @param
	 * @param
	 * @return
	 */
	public int withdraw(Dto dto){
		ApiUtils.checkParam(dto,"userId,accountId,title,money,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+BigDecimal.ZERO.subtract(dto.getBigDecimal("money")));
		order.setPoints(dto.getBigDecimal("points"));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO.subtract(dto.getBigDecimal("money")));
		notice.setPoints(BigDecimal.ZERO);
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");// 0
		notice.setMemo(dto.getString("title")+"："+dto.getBigDecimal("money"));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("1");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}






	/**
	 * 查询sn单条记录
	 *
	 * @param sn
	 * @return
	 */
	public T_order_noticePO selectBySn(String sn) {
		Dto dto= Dtos.newDto("sn",sn);
		T_order_noticePO row = (T_order_noticePO) sqlDao.selectOne("OrderNotice.selectBySn",dto);
		return row;
	}
	


	
	/**
	 * 查询单条记录
	 * 
	 * @param id
	 * @return
	 */
	public T_orderPO selectByPrimaryKey(String id) {
		T_orderPO row = orderDao.selectByKey(id);
		return row;
	}
	
	/**
	 * 修改
	 * 
	 * @param record
	 * @return
	 */
	public Integer updateByPrimaryKey(T_orderPO record) {
		return orderDao.updateByKey(record);
	}
	
	/**
	 * 查询某记录是否存在
	 * 
	 * @param
	 * @return
	 */
	public List<T_orderPO> selectByDto(Dto dto) {
		List<T_orderPO> row = sqlDao.list("TOrder.selectByDto", dto);
		return row;
	}


	/**
	 * 获取账单
	 * @param dto
	 * @return
	 */
	public List<Dto> getNoticeList(Dto dto) {
		return sqlDao.list("OrderNotice.getNoticeList",dto);
	}

	/**
	 * 余额消耗
	 * @param dto
	 * @return
	 */
	public Integer deduMoney(Dto dto) {
		ApiUtils.checkParam(dto,"userId,accountId,title,money,typeId,sn");
		T_orderPO order =new T_orderPO();
		order.setId(SystemIdUtils.uuid());
		order.setUser_id(dto.getString("userId"));
		order.setIs_settle("0");
		order.setMemo(dto.getString("title")+":"+BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		order.setMoney(BigDecimal.ZERO.subtract(dto.getBigDecimal("money")));
		order.setType_id(dto.getString("typeId"));
		order.setCreate_time(new Date());
		order.setIs_valid("1");
		orderDao.insert(order);
		T_order_noticePO notice =new T_order_noticePO();
		notice.setId(SystemIdUtils.uuid());
		notice.setUser_id(dto.getString("userId"));
		notice.setTitle(dto.getString("title"));
		notice.setMoney(BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		notice.setPoints(BigDecimal.ZERO);
		notice.setAccount_id(dto.getString("accountId"));
		notice.setPayment_code("0");
		notice.setMemo(dto.getString("title")+":"+BigDecimal.ZERO.subtract(dto.getBigDecimal("points")));
		notice.setOrder_type(dto.getString("typeId"));
		notice.setIs_paid("0");
		notice.setSn(dto.getString("sn"));
		notice.setCreate_time(new Date());
		notice.setIs_valid("1");
		return orderNoticeDao.insert(notice);
	}
}
